<?php 

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class Scoreboard {

	private $page;
	private $alerter;

	public function __construct($page) {
		$this->page = $page;	
		$this->alerter = new Alerter();
	}

	public function show($type) {
		if (!$_SESSION['user_id'])
			errorpage(6);

		// check-fetch type properties
		$type_props = db_fetch_array(db_do_query($page, db_query_select(array(array('*')), array('ITEM_TYPE'), "extension = '$type'")));

		if ($type_props['can_be_scored']) {
			$this->score_what_can_be_scored($type_props);
		
		} else {
			$this->alerter->add_alert("quoteringen zijn niet toegestaan voor dit item type"); //TODO: internationalise
		}
		return $this->page;
	}

	private function js_scoreboard($type_props, $prevscore) {
		//TODO: extract Javascript
		return "<script type='text/javascript'> 
				function scoreClick(n) {
					if ((document.getElementById('score').value == 1) && (n == 1))
						n = 0;
					document.getElementById('score').value = n;
					for (var i = 1; i <= {$type_props['can_be_scored']}; i++)
						document.getElementById('score_' + i).src = '/gfx/score_' + ((n >= i) ? 1 : 0) + '.gif';
				}
			</script>
			<input type=\"hidden\" id=\"score\" name=\"score\" value=\"" . intval($prevscore['score']) . "\" /> ";
	}

	private function check_score() {

		$checker = new InputChecker();
		return $_POST['submitted'] && $_POST['score'] 
				&& $checker->check_tags($_POST['comments'], "commentaar");
	}

	private function score_what_can_be_scored($type_props) {
		$sqlres = db_do_query($page, db_query_select(array(array('*')), array('ITEM_SCORE'), 
							  "item_id = {$this->page['id']} AND user_id = {$_SESSION['user_id']}"));

		if ($this->check_score()) {
			if (db_num_rows($sqlres) == 0) {
				db_do_query($page, db_query_insert(array('item_id', 'user_id', 'score', 'comments'), 
											array($this->page['id'], $_SESSION['user_id'], $_POST['score'], 
											db_sqlize($_POST['comments'])), "ITEM_SCORE"));
			}
			else {
				db_do_query($page, db_query_update(array('score', 'comments'), array($_POST['score'], 
											db_sqlize($_POST['comments'])), "ITEM_SCORE", "item_id = {$this->page['id']} AND user_id = {$_SESSION['user_id']}"));
			}
				
			add_alert("OK, je score is bewaard."); //TODO: internationalise

		} else {
			
			$prevscore = (db_num_rows($sqlres) > 0) ? db_fetch_array($sqlres) : array();
			
			$inputs = array();
			
			$creator = new FormCreator();
			if ($type_props['can_be_commented']) {
				$inputs[] = $creator->input_text("Commentaar", "comments", $prevscore['comments'], 3);
			}
			
			$levels = implode("</li><li>", explode("#", $type_props['score_levels']));
			$inputs[] = $creator->info("<ol start=0><li>{$levels}</li></ol>");
		
			$board = $this->js_scoreboard($type_props, $prevscore);
			for ($i = 1; $i <= $type_props['can_be_scored']; $i++)
				$board .= "<a href=javascript:scoreClick($i)><img id=score_$i src=/gfx/score_" . (($prevscore['score'] >= $i) ? 1 : 0) . ".gif /></a> ";
				//TODO: remove obtrusive javascript in link
			
			$inputs[] = $creator->info($board, 0, 0, "Score");
			$inputs[] = $creator->header($type_props['score_title']);
			
			$this->page['scoreform'] = $creator->create("scoreform", $inputs);
		}				
	}
}
?>
